package com.hdoit.ares.logical.official.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.official.dao.IArticalDao;
import com.hdoit.ares.logical.official.model.Artical;
import com.hdoit.ares.logical.official.model.PraiseAndGlance;
import com.hdoit.ares.logical.viewmodel.official.ArticalSearch;
import com.hdoit.ares.selfcommon.enums.official.TypeOfPraise;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年01月10日 09:53:47
 * @comment
 */
@Component
public class ArticalDaoImpl extends SuperDao<Artical> implements IArticalDao{

	@Override
	public List<Artical> queryList(ArticalSearch search, PagerModel pagerModel) {
        IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("*")
		                .from(Artical.class)
		                .addOrder("createTime", OrderEnum.desc);
		
		appendFinder(search,finderExpression);

        if(pagerModel == null){
            return queryList(finderExpression, search); 
          }
        return queryList(finderExpression, search, pagerModel);
	}

	private void appendFinder(ArticalSearch search, IFinderExpression finder) {
		if(search.getStartDate()!=null){
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},createTime)>=0")
				  .setParam("startDate", search.getStartDate());
		}
		if(search.getEndDate()!=null){
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},createTime)<=0")
				  .setParam("endDate", search.getEndDate());
		}
		
		if(!StringUtils.isEmpty(search.getImportantType())){
			finder.andWhere("importantType = #{importantType}")
				  .setParam("importantType", search.getImportantType());
		}
		
		if(!StringUtils.isEmpty(search.getTypeId())){
			finder.andWhere("typeId = #{typeId}")
				  .setParam("typeId", search.getTypeId());
		}
		
		if(!StringUtils.isEmpty(search.getId())){
			finder.andWhere("id = #{id}")
				  .setParam("id", search.getId());
		}
		
		
	}

	@Override
	public List<Artical> queryList(ArticalSearch search) {
		return queryList(search,null);
	}

	@Override
	public int queryCount(ArticalSearch search) {
		    IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
			
			finderExpression.select("count(id) as count")
	        				.from(Artical.class);
			
			appendFinder(search,finderExpression);
			
			return queryCount(finderExpression, search);
	}

	@Override
	public Artical queryArticalById(String id) {
		
		   IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
			
		   finderExpression.select("*")
			               .from(Artical.class)
		 	               .andEq("id", id);
		  
		   return queryModel(finderExpression);
	}

	@Override
	public List<Artical> queryListArtical(String typeId) {
		   
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		   finderExpression.select("*")
			               .from(Artical.class)
		 	               .andEq("typeId", typeId);
		return queryList(finderExpression);
	}

	@Override
	public List<Artical> queryArticals(ArticalSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("artical.*","praiseAndGlance.praiseCount as praiseCount","praiseAndGlance.glanceCount as glanceCount")
						.from(Artical.class,"artical")
						.leftJoin(PraiseAndGlance.class,"praiseAndGlance")
						.on("artical.id = praiseAndGlance.receiveId")
						.andWhere("praiseAndGlance.type = #{type}")
						.setParam("type", TypeOfPraise.Medio)
						.addOrder("artical.createTime", OrderEnum.desc);
		
		appendFinderExpression(search, finderExpression);
		
		return queryList(finderExpression);
	}

	private void appendFinderExpression(ArticalSearch search, IFinderExpression finderExpression) {
		if(search.getStartDate()!=null){
			finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},artical.createTime)>=0")
				  			.setParam("startDate", search.getStartDate());
		}
		if(search.getEndDate()!=null){
			finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},artical.createTime)<=0")
				  			.setParam("endDate", search.getEndDate());
		}
		
		if(!StringUtils.isEmpty(search.getImportantType())){
			finderExpression.andWhere("artical.importantType = #{importantType}")
				  			.setParam("importantType", search.getImportantType());
		}
		
		if(!StringUtils.isEmpty(search.getTypeId())){
			finderExpression.andWhere("artical.typeId = #{typeId}")
				  			.setParam("typeId", search.getTypeId());
		}
		
		if(!StringUtils.isEmpty(search.getId())){
			finderExpression.andWhere("artical.id = #{id}")
				  			.setParam("id", search.getId());
		}
		
/*		if(search.getPageNo()!=null){
			finderExpression.limit((search.getPageNo() - 1)*search.getPageSize(), search.getPageSize());
		}else{
			finderExpression.limit(0, search.getPageSize());
		}*/
		
		
	}
	
}